<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import {userInfoService}from '@/api/user.js'
import {useInfoStore} from '@/stores/userinfo.js'
import {ElMessageBox, ElMessage} from 'element-plus'
import router from '@/router/index.js'
const infoStore = useInfoStore();
//获取用户的基本信息
const userInfo = async () => {
    let res = await userInfoService();
    infoStore.setUseInfo(res.data);
    console.log(infoStore.useInfo);
};
//条目被点击后触发的
const handleCommand=(command)=>{
    if(command==='logout'){
        //点击的退出登录
        ElMessageBox.confirm('确定要退出登录吗？','提示',{
            confirmButtonText:'确定',
            cancelButtonText:'取消',
            type:'warning'
        }).then(()=>{
                ElMessage.success('退出成功');
                router.push('/login');
        }).catch(()=>{
            //点击取消
            ElMessage.info('取消退出');
        })
    }
    router.push(`/user/${command}`)
}

userInfo();
</script>
<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <h3 style="text-align:center; color: antiquewhite;">清泓智巡</h3>
            <div class="el-aside__logo"></div>
            <el-menu active-text-color="#ffd04b" background-color="#232323"  text-color="#fff"
                router>
                <el-menu-item index="/index">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>首页</span>
                </el-menu-item>
                <!-- <el-menu-item index="/article/category">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>文章分类</span>
                </el-menu-item>
                <el-menu-item index="/article/manage">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>文章管理</span>
                </el-menu-item> -->
                <el-menu-item index="/boat/monitor">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>监控</span>
                </el-menu-item>
                <el-menu-item index="/boat/map">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>地图</span>
                </el-menu-item>
                 <el-menu-item index="/solar">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>太阳能</span>
                </el-menu-item>
                <el-menu-item index="/boat/sensor">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>实时参数</span>
                </el-menu-item>
                <el-menu-item index="/AI">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>AI助手</span>
                </el-menu-item>
                <el-sub-menu >
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/user/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/user/avatar">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>更换头像</span>
                    </el-menu-item>
                    <!-- <el-menu-item index="/user/password">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>重置密码</span>
                    </el-menu-item> -->
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>尊贵的：<strong>{{ infoStore.useInfo.username }}</strong></div>
                <!-- 
                下拉菜单 command：条目贝蒂那几后触发 路由跳转
                -->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="infoStore.useInfo.userPic?infoStore.useInfo.userPic:avatar" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="avatar" :icon="Crop">更换头像</el-dropdown-item>
                            <!-- <el-dropdown-item command="password" :icon="EditPen">重置密码</el-dropdown-item> -->
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                    <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
            <el-footer>清泓智巡 ©2025 Created by 宋浩</el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #232323;

        &__logo {
            height: 120px;
            background: url('../assets/logo.jpg') no-repeat center / 120px auto;
        }

        .el-menu {
            border-right: none;
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #999;
                margin-left: 10px;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
    }
}
</style>